package com.gztms.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.gztms.enums.AppCodeEnum;

/**
 * @author Zoey
 * @date 2023/3/7 15:43
 * @Description 请求响应工具
 **/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultUtil<T> {

    /**
     * 请求成功
     */
    public final static Integer SUCCESS = 200;
    /**
     * 错误请求，用户必填信息不能为空
     */
    public final static Integer USER_INFO_NULL = 410;
    /**
     * 错误请求，用户账号长度错误
     */
    public final static Integer USER_NUMBER_ERROR = 411;
    /**
     * 错误请求，密码不符合要求
     */
    public final static Integer PASSWORD_ERROR = 412;
    /**
     * 错误请求，手机号填写错误
     */
    public final static Integer PHONE_NUMBER_ERROR = 413;
    /**
     * 错误请求，该用户已存在
     */
    public final static Integer USER_EXISTED = 414;
    /**
     * 服务器请求错误
     */
    public final static Integer SERVER_ERROR = 500;

    /**
     * 状态码
     */
    private Integer code;
    /**
     * 提示信息，如果有错误时，前端可以获取该字段进行提示
     */
    private String message;
    /**
     * 查询到的结果数据，
     */
    private T data;



    public ResultUtil(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResultUtil(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public ResultUtil() {
        this.code = AppCodeEnum.SUCCESS.getCode();
        this.message = AppCodeEnum.SUCCESS.getMsg();

    }

    public static ResultUtil errorResult(int code, String msg){
        ResultUtil resultUtil = new ResultUtil();
        return resultUtil.error(code,null, msg);
    }

    public static ResultUtil okResult(){
        ResultUtil resultUtil = new ResultUtil();
        return resultUtil;
    }

    public static ResultUtil okResult(int code, String msg){
        ResultUtil resultUtil = new ResultUtil();
        return resultUtil.ok(code,null,msg);
    }

    public static ResultUtil okResult(Object data){
        ResultUtil resultUtil = setAppCodeEnum(AppCodeEnum.SUCCESS, AppCodeEnum.SUCCESS.getMsg());
        if (data!=null){
            resultUtil.setData(data);
        }
        return resultUtil;
    }

    public ResultUtil<?> ok(T data) {
        this.data = data;
        return this;
    }
    public ResultUtil<?> ok(Integer code, T data) {
        this.code = this.code;
        this.data = data;
        return this;
    }

    public ResultUtil<?> ok(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.message = msg;
        return this;
    }

    public ResultUtil<?> error(T data) {
        this.data = data;
        return this;
    }
    public ResultUtil<?> error(int code, T data) {
        this.code = code;
        this.data = data;
        return this;
    }

    public ResultUtil<?> error(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.message = msg;
        return this;
    }

    public static ResultUtil errorResult(AppCodeEnum codeEnum){
        return setAppCodeEnum(codeEnum, codeEnum.getMsg());
    }
    public static ResultUtil errorResult(AppCodeEnum codeEnum, String message){
        return setAppCodeEnum(codeEnum, message);
    }


    public static ResultUtil setAppCodeEnum(AppCodeEnum codeEnum){
        return okResult(codeEnum.getCode(), codeEnum.getMsg());
    }

    public static ResultUtil setAppCodeEnum(AppCodeEnum codeEnum, String msg){
        return okResult(codeEnum.getCode(), msg);
    }


}
